package com.app.util;

import java.util.ArrayList;
import java.util.Stack;

import com.app.model.AlarmDataManager;
import com.app.model.AlarmDataManager.AlarmLoader;
import com.app.view_controller.ContentActivity;

import android.R.integer;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.AudioManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Class that handles the the volume changes for the application.
 * 
 * @author Nick
 * 
 */
public class VolumeManager {

	private static final String DEFAULT_RINGTONE_VOLUME = "default ringtone volume";
	private static final String DEFAULT_NOTIFICATION_VOLUME = "default notification volume";
	// TODO sistemare vibrazione
	private static final String DEFAULT_VIBRATION_SETTINGS = "vibration default settings";
	private static final String VOLUMES_COUNTER = "should set default volumes";
	private static final String DEFAULT_VOLUME_LEVELS = "default volume levels";
	/**
	 * The audio stream for the phone ring (Same value of
	 * {@link AudioManager#STREAM_RING})
	 */
	public static final int STREAM_RING = AudioManager.STREAM_RING;
	/**
	 * The audio stream for notifications (Same value of
	 * {@link AudioManager#STREAM_NOTIFICATION})
	 */
	public static final int STREAM_NOTIFICATION = AudioManager.STREAM_NOTIFICATION;
	public static final int STREAM_ALARM = AudioManager.STREAM_ALARM;
	private static final String DEFAULT_ALARM_VOLUME = "default alarm volume";
	private static final boolean DEBUG = true;
	private static final String TAG = "VOLUME_MAMANGER";

	private Context mContext;
	private AudioManager mAudio;
	private SharedPreferences mPreferences;
	private Editor mEditor;
	private static int mCounter;
	private static VolumeManager instance;

	/**
	 * 
	 * @param context
	 *            The activity {@link Context}
	 */
	private VolumeManager(Context context) {
		mContext = context;
		mAudio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
		mPreferences = mContext.getSharedPreferences(DEFAULT_VOLUME_LEVELS, Context.MODE_PRIVATE);
		mEditor = mPreferences.edit();
		mCounter = mPreferences.getInt(VOLUMES_COUNTER, 0);
	}

	public static VolumeManager getVolumeManagerInstance(Context context) {
		if (instance == null)
			instance = new VolumeManager(context);
		return instance;
	}

	public void setAlarmVolume(int level) {
		mCounter++;
		if (DEBUG)
			Log.d(TAG, "setalarmvolume   should save volume " + mPreferences.getBoolean(VOLUMES_COUNTER, false));
		mAudio.setStreamVolume(STREAM_ALARM, level, 0);
	}

	/**
	 * This method will set the System volume level for the specified
	 * streamType.
	 * 
	 * @param streamType
	 *            The streamType of the volume that must be handle such as
	 *            {@link VolumeManager#STREAM_RING}
	 * @param level
	 *            The volume level as an {@link integer} number.
	 */
	public void setStreamVolume(int streamType, int level, int flags) {
		// These new volume levels mustn't be saved as default Volumes in
		// VolumeChangedReceiver
		mCounter++;
		if (DEBUG)
			Log.d(TAG, "setstreamvolume   should save volume " + mPreferences.getInt(VOLUMES_COUNTER, 0));
		// Set the new volume level
		if (streamType == STREAM_RING)
			mAudio.setStreamVolume(streamType, level, flags);
		if (streamType == STREAM_NOTIFICATION)
			mAudio.setStreamVolume(streamType, level, flags);
		if (streamType == STREAM_ALARM)
			mAudio.setStreamVolume(streamType, level, flags);
	}

	public int getStreamVolume(int streamType) {
		return mAudio.getStreamVolume(streamType);
	}

	/**
	 * It restores the ringtone or notification volume level that was set before
	 * the manipulations of this application.
	 * 
	 * @param streamType
	 *            The streamType of the volume that must be handle such as
	 *            {@link VolumeManager#STREAM_RING}
	 */
	public void restoreDefaultVolumeLevel(int streamType) {
		mCounter++;
		if (streamType == STREAM_RING)
			mAudio.setStreamVolume(streamType, mPreferences.getInt(DEFAULT_RINGTONE_VOLUME, 0), 0);
		if (streamType == STREAM_NOTIFICATION)
			mAudio.setStreamVolume(streamType, mPreferences.getInt(DEFAULT_NOTIFICATION_VOLUME, 0), 0);
		if (streamType == STREAM_ALARM)
			mAudio.setStreamVolume(streamType, mPreferences.getInt(DEFAULT_ALARM_VOLUME, 0), 0);
		if (DEBUG)
			Log.d(TAG, "restore default volume levels " + mAudio.getStreamVolume(streamType));
	}

	public boolean areVolumesSafe() {
		if (mCounter > 0)
			return false;
		return true;
	}

	public void decreaseCounter() {
		if (mCounter > 0) {
			mCounter--;
			mEditor.putInt(VOLUMES_COUNTER, mCounter);
			mEditor.commit();
		}
	}

	/**
	 * It saves the default volume level for ringtones and notifications. If the
	 * last volume manipulation has been done using the method
	 * {@link VolumeManager#setStreamVolume(int, int)} this method doesn't save
	 * the new values because they are not the default ones.
	 */
	public void saveDefaultVolumeLevels(boolean autoDecreaseCounter) {
		if (mCounter <= 0) {
			if (DEBUG)
				Log.d(TAG, "savedefaultvolume counter  " + mPreferences.getInt(VOLUMES_COUNTER, 0));
			mEditor.putInt(DEFAULT_RINGTONE_VOLUME, mAudio.getStreamVolume(AudioManager.STREAM_RING));
			mEditor.putInt(DEFAULT_NOTIFICATION_VOLUME, mAudio.getStreamVolume(AudioManager.STREAM_NOTIFICATION));
			mEditor.putInt(DEFAULT_ALARM_VOLUME, mAudio.getStreamVolume(AudioManager.STREAM_ALARM));
			if (DEBUG)
				Log.d(TAG,
						"save default volume levels /n ring " + mAudio.getStreamVolume(AudioManager.STREAM_RING) + " not "
								+ mAudio.getStreamVolume(AudioManager.STREAM_NOTIFICATION) + " alarm "
								+ mAudio.getStreamVolume(AudioManager.STREAM_ALARM));

		}
		if (autoDecreaseCounter)
			decreaseCounter();

	}

}
